package com.example.common.handler;

import com.example.common.exception.GlobalException;
import com.example.common.msg.ErrorMsgEnum;
import com.example.common.msg.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常处理器
 * @author hutu
 * @date 2018/3/28 10:21
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(GlobalException.class)
	public R handleRRException(GlobalException e){
		R r = new R();
		r.put(R.CODE, e.getCode());
		r.put(R.MSG, e.getMessage());

		return r;
	}

	@ExceptionHandler(NullPointerException.class)
	public R nullPointerException(NullPointerException e){
		logger.error(e.getMessage(), e);
		return R.error(ErrorMsgEnum.NULL_POINTER_EXCEPTION);
	}


	@ExceptionHandler(Exception.class)
	public R handleException(Exception e){
		logger.error(e.getMessage(), e);
		return R.error();
	}
}
